package com.liuwa.framework.web.service;

import com.liuwa.common.core.domain.entity.SysUser;
import com.liuwa.common.core.domain.model.LoginUser;
import com.liuwa.system.service.SysUserService;
import com.liuwa.system.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author: Liuwa
 * @Description:
 * @Date: 2024/2/2
 */
@Service
public class SysUserTokenServiceImpl implements SysUserTokenService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysUserService userService;



    @Autowired
    private SysPermissionService permissionService;

    @Override
    public void kick(Long userId) {
        tokenService.kick(userId);
    }


    @Override
    public void refreshLoginUser(Long userId) {

        List<LoginUser> loginUsers = tokenService.findLoginUserByUserId(userId);
        if(loginUsers.size() > 0){
            SysUser user = userService.selectUserById(userId);


            Set<String> permissions = permissionService.getMenuPermission(user);

            for(LoginUser loginUser : loginUsers){

                loginUser.setDeptId(user.getDeptId());
                loginUser.setPermissions(permissions);
                loginUser.setUser(user);

                tokenService.refreshToken(loginUser);
            }
        }
    }
}
